package com.bite.trans.controller;

import com.bite.trans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RequestMapping("/trans")
@RestController
public class TransController {

    @Autowired
    private UserService userService;

    /**
     * 事务回滚
     * @param name
     * @param password
     * @return
     */
    @Transactional
    @RequestMapping("/r1")
    public Boolean registry(String name, String password){
        Integer result = userService.insertUser(name, password);
        log.info("数据影响行数: "+ result);

        int a = 10/0;
        return true;
    }

    /**
     * 事务提交
     * @param name
     * @param password
     * @return
     * @throws IOException
     */
    @Transactional
    @RequestMapping("/r2")
    public Boolean r2(String name, String password) throws IOException {
        Integer result = userService.insertUser(name, password);
        log.info("数据影响行数: "+ result);

        if (true){
            throw new IOException();
        }
        return true;
    }

    /**
     * 事务 回滚
     * @param name
     * @param password
     * @return
     * @throws IOException
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.DEFAULT)
    @RequestMapping("/r3")
    public Boolean r3(String name, String password) throws IOException {
        Integer result = userService.insertUser(name, password);
        log.info("数据影响行数: "+ result);

        if (true){
            throw new IOException();
        }
        return true;
    }

    /**
     * 事务提交
     * @param name
     * @param password
     * @return
     */
    @Transactional
    @RequestMapping("/r5")
    public Boolean r5(String name, String password){
        Integer result = userService.insertUser(name, password);
        log.info("数据影响行数: "+ result);
        try {
            int a = 10/0;
        }catch (Exception e){
            log.error("发生异常");
        }

        return true;
    }

    /**
     * 事务 回滚
     * @param name
     * @param password
     * @return
     */
    @Transactional
    @RequestMapping("/r6")
    public Boolean r6(String name, String password){
        Integer result = userService.insertUser(name, password);
        log.info("数据影响行数: "+ result);
        try {
            int a = 10/0;
        }catch (Exception e){
            throw e;
        }

        return true;
    }

    /**
     * 事务 回滚
     * @param name
     * @param password
     * @return
     */
    @Transactional
    @RequestMapping("/r7")
    public Boolean r7(String name, String password){
        Integer result = userService.insertUser(name, password);
        log.info("数据影响行数: "+ result);
        try {
            int a = 10/0;
        }catch (Exception e){
            //手动设置回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return true;
    }
}
